<template>
  <div class="meter_analysis">
    <div style="display: flex; flex-wrap: nowrap; width: 100%">
      <!-- 监理计量 -->
      <supervisor :projectId="projectId" :data="supervisorData" />
      <!-- 施工计量(万元) -->
      <construction :projectId="projectId" :data="constructionData" @onMonthChanged="onMonthChanged" />
    </div>

    <!-- 项目变更 -->
    <projectChange :projectId="projectId" :dataList="projecChangeList" @onProjectTimeChanged="onProjectTimeChanged" />

    <!-- 服务合同计量 -->
    <!--    <serviceContract :projectId="projectId" :dataList="contractDataList" />-->
  </div>
</template>
<script>
import * as echarts from 'echarts';
// 真实数据接口，改为真实数据接口后，在接口调用代码处做对应修改
// import supervisorMetaphaseMeasureAPI from '@/api/buss/supervisionMeasurement/supervisorMetaphaseMeasure';
// import contractAPI from '@/api/buss/contract';
import * as theAPI from './meterAnalysisAPI.js'; //临时演示数据接口

import supervisor from './components/supervisor.vue';
import construction from './components/construction.vue';
import projectChange from './components/projectChange.vue';
import serviceContract from './components/serviceContract.vue';

export default {
  name: 'meterAnalysis',
  components: {
    supervisor,
    construction,
    projectChange,
    serviceContract
  },
  data() {
    return {
      // 监视计量
      supervisorData: {
        measureSum: 0,
        contractSum: 0,
        activeName: '',
        list: []
      },

      // 施工计量
      constructionData: {
        month: new Date(`${new Date().getFullYear()}-${new Date().getMonth()}`),
        activeName: '',
        contractSum: 0, //合同金额
        measureSum: 0, //本月计量
        percent: 0, //百分比
        list: []
      },

      // 项目变更
      projecChangeList: [],

      // 服务合同数据
      contractDataList: []
    };
  },
  computed: {
    // 多项目 / 单项目 （projectId值是projectId存在值为单项目）
    projectId() {
      console.log(this.$store.state.settings)
      return this.$store.state.settings.changeHeaderProject;
      // return '112345'; // 单项目，项目编号
      // return null; // 多项目
    },
  },
  watch: {
    projectId: {
      handler(val) {
        this.loadSupervisorData();
        this.loadConstructionData();
        this.loadProjectChangeData();
        this.loadContractData();
      },
      immediate: true
    }
  },

  methods: {
    loadSupervisorData() {
      const params = {
        projectId: this.projectId
      };
      //supervisorMetaphaseMeasureAPI
      theAPI.getWorkbenchList(params).then(res => {
        if (res.code === 200) {
          // 后端给的金额单位是元 前端根据页面显示的单位自行换算
          // 2023-11-01 后端要求前端给什么数据就展示什么，不用再调整
          this.supervisorData = {
            measureSum: 0,
            contractSum: 0,
            activeName: '',
            list: []
          };
          this.supervisorData.contractSum = res.data.contractSum;
          this.supervisorData.measureSum = res.data.measureSum;
          this.supervisorData.list = res.data.list;
          if (this.supervisorData.list.length > 0) {
            this.supervisorData.activeName = this.supervisorData.list[0].name;
          }
        }
      });
    },

    // 施工计量的月份选择更改时
    onMonthChanged(month) {
      this.loadConstructionData(month);
    },
    loadConstructionData(month) {
      if (!month) {
        month = this.constructionData.month;
      }
      const params = {
        projectId: this.projectId,
        month: this.timeFormat(month, 'yyyy-MM')
      };

      //this.$httpPost('/buss-measure/measurementPayment/getWorkbenchList', params)
      theAPI
        .getConstructionData(params)
        .then(res => {
          if (res.code === 200) {
            this.constructionData = {
              month: new Date(`${new Date().getFullYear()}-${new Date().getMonth()}`),
              activeName: '',
              contractSum: 0, //合同金额
              measureSum: 0, //本月计量
              percent: 0, //百分比
              list: []
            };
            this.constructionData.list = res.data;
          }
        })
        .catch(err => {
          console.log(err);
        });
    },

    //projectTime: '累计' '本年' '本季'
    onProjectTimeChanged(projectTime) {
      this.loadProjectChangeData(projectTime);
    },
    loadProjectChangeData(projectTime) {
      projectTime = projectTime || '累计';
      theAPI.getProjecChangeList(projectTime).then(result => {
        this.projecChangeList = []; //建议清空重置，子组件能监视
        this.projecChangeList = result.data;
      });
    },

    loadContractData() {
      const params = {
        projectId: this.projectId
      };
      //contractAPI
      theAPI.serviceContractMeasure(params).then(res => {
        if (res.code === 200) {
          this.contractDataList = [];
          this.contractDataList = res.data;
        }
      });
    },

    timeFormat(Date, fmt = 'yyyy-MM-dd') {
      // "yyyy-MM-dd hh:mm:ss.S" ==> 2006-07-02 08:09:04.423
      // "yyyy年M-d h:m:s.S"      ==> 2006年7-2 8:9:4.18
      if (typeof Date === 'string') {
        Date = new window.Date(Date.replace(/-/g, '/'));
      }
      if (!(Date instanceof window.Date)) {
        console.log('时间格式化失败');
        return '';
      }
      const o = {
        'M+': Date.getMonth() + 1,
        'd+': Date.getDate(),
        'h+': Date.getHours(),
        'm+': Date.getMinutes(),
        's+': Date.getSeconds(),
        'q+': Math.floor((Date.getMonth() + 3) / 3)
        // 'S': Date.getMilliseconds()
      };
      if (/(y+)/.test(fmt)) {
        fmt = fmt.replace(RegExp.$1, (Date.getFullYear() + '').substr(4 - RegExp.$1.length));
      }
      for (const k in o) {
        if (new RegExp('(' + k + ')').test(fmt)) {
          fmt = fmt.replace(RegExp.$1, RegExp.$1.length === 1 ? o[k] : ('00' + o[k]).substr(('' + o[k]).length));
        }
      }
      return fmt;
    }
  }
};
</script>
<style lang="scss" scoped>
.meter_analysis {
  font-family: Microsoft YaHei-Regular;
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
}
</style>
